Code
::p_load(lubridate, ggthemes, reactable,
pacman reactablefmtr, gt, gtExtras, tidyverse)
Enrico Sebastian
June 25, 2025
June 27, 2025
By the end of this hands-on exercise, you will be able to:
create bullet chart by using ggplot2,
create sparklines by using ggplot2 ,
build industry standard dashboard by using R Shiny.
For the purpose of this hands-on exercise, the following R packages will be used.
tidyverse provides a collection of functions for performing data science task such as importing, tidying, wrangling data and visualising data. It is not a single package but a collection of modern R packages including but not limited to readr, tidyr, dplyr, ggplot, tibble, stringr, forcats and purrr.
lubridate provides functions to work with dates and times more efficiently.
ggthemes is an extension of ggplot2. It provides additional themes beyond the basic themes of ggplot2.
gtExtras provides some additional helper functions to assist in creating beautiful tables with gt, an R package specially designed for anyone to make wonderful-looking tables using the R programming language.
reactable provides functions to create interactive data tables for R, based on the React Table library and made with reactR.
reactablefmtr provides various features to streamline and enhance the styling of interactive reactable tables with easy-to-use and highly-customizable functions and themes.
For the purpose of this study, a personal database in Microsoft Access mdb format called Coffee Chain will be used.
In the code chunk below, odbcConnectAccess()
of RODBC package is used used to import a database query table into R.
Note: Before running the code chunk, you need to change the R system to 32bit version. This is because the odbcConnectAccess()
is based on 32bit and not 64bit
The code chunk below is used to import CoffeeChain.rds into R.
Note: This step is optional if coffeechain is already available in R.
The code chunk below is used to aggregate Sales and Budgeted Sales at the Product level.
The code chunk below is used to plot the bullet charts using ggplot2 functions.
ggplot(product, aes(Product, current)) +
geom_col(aes(Product, max(target) * 1.01),
fill="grey85", width=0.85) +
geom_col(aes(Product, target * 0.75),
fill="grey60", width=0.85) +
geom_col(aes(Product, target * 0.5),
fill="grey50", width=0.85) +
geom_col(aes(Product, current),
width=0.35,
fill = "black") +
geom_errorbar(aes(y = target,
x = Product,
ymin = target,
ymax= target),
width = .4,
colour = "red",
size = 1) +
coord_flip()
In this section, you will learn how to plot sparklines by using ggplot2.
The code chunk below is used to compute the minimum, maximum and end othe the month sales.
The code chunk below is used to compute the 25 and 75 quantiles.
The code chunk used.
ggplot(sales_report, aes(x=Month, y=Sales)) +
facet_grid(Product ~ ., scales = "free_y") +
geom_ribbon(data = quarts, aes(ymin = quart1, max = quart2),
fill = 'grey90') +
geom_line(size=0.3) +
geom_point(data = mins, col = 'red') +
geom_point(data = maxs, col = 'blue') +
geom_text(data = mins, aes(label = Sales), vjust = -1) +
geom_text(data = maxs, aes(label = Sales), vjust = 2.5) +
geom_text(data = ends, aes(label = Sales), hjust = 0, nudge_x = 0.5) +
geom_text(data = ends, aes(label = Product), hjust = 0, nudge_x = 1.0) +
expand_limits(x = max(sales_report$Month) +
(0.25 * (max(sales_report$Month) - min(sales_report$Month)))) +
scale_x_continuous(breaks = seq(1, 12, 1)) +
scale_y_continuous(expand = c(0.1, 0)) +
theme_tufte(base_size = 3, base_family = "Helvetica") +
theme(axis.title=element_blank(), axis.text.y = element_blank(),
axis.ticks = element_blank(), strip.text = element_blank())
In this section, you will learn how to create static information dashboard by using gt and gtExtras packages. Before getting started, it is highly recommended for you to visit the webpage of these two packages and review all the materials provided on the webpages at least once. You done not have to understand and remember everything provided but at least have an overview of the purposes and functions provided by them.
In this section, you will learn how to prepare a bullet chart report by using functions of gt and gtExtras packages.
Before we can prepare the sales report by product by using gtExtras functions, code chunk below will be used to prepare the data.
It is important to note that one of the requirement of gtExtras functions is that almost exclusively they require you to pass data.frame with list columns. In view of this, code chunk below will be used to convert the report data.frame into list columns.
First, calculate summary statistics by using the code chunk below.
Next, use the code chunk below to add the statistics on the table.
Similarly, we can combining the bullet chart and sparklines using the steps below.
In this section, you will learn how to create interactive information dashboard by using reactable and reactablefmtr packages. Before getting started, it is highly recommended for you to visit the webpage of these two packages and review all the materials provided on the webpages at least once. You done not have to understand and remember everything provided but at least have an overview of the purposes and functions provided by them.
In order to build an interactive sparklines, we need to install dataui R package by using the code chunk below.
Next, you all need to load the package onto R environment by using the code chunk below.
Similar to gtExtras, to plot an interactive sparklines by using reactablefmtr package we need to prepare the list field by using the code chunk below.
Next, react_sparkline will be to plot the sparklines as shown below.
By default the pagesize is 10. In the code chunk below, arguments defaultPageSize is used to change the default setting.
In the code chunk below highlight_points
argument is used to show the minimum and maximum values points and label
argument is used to label first and last values.
In the code chunk below statline
argument is used to show the mean line.
Instead adding reference line, bandline can be added by using the bandline
argument.
Instead of displaying the values as sparklines, we can display them as sparkbars as shiwn below.